﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TED.Model;
using System.Xml.Linq;
using System.Xml;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Phone.Controls;
using TED.ViewModels;
using System.Windows.Navigation;

namespace TED.View
{
    public partial class ItemView : UserControl
    {
        private FavouriteVideoModel fvm;
        private VideoViewModel vvm;


        public ItemView()
        {
            InitializeComponent();
            fvm = new FavouriteVideoModel();
            vvm = new VideoViewModel();
        }

        private void AddFavourite_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem selectedListBoxItem = this.listBox.ItemContainerGenerator.ContainerFromItem
                ((sender as MenuItem).DataContext) as ListBoxItem;

            if (selectedListBoxItem == null)
            {
                return;
            }
            string author = ((TEDVideo) selectedListBoxItem.Content).Author.ToString();
            string viewUrl = ((TEDVideo) selectedListBoxItem.Content).ViewUrl.ToString();
            Uri imageUrl = new Uri(((TEDVideo) selectedListBoxItem.Content).ImageUrl.ToString());
            string title = ((TEDVideo) selectedListBoxItem.Content).Title.ToString();
            string videoUrl;
            if (((TEDVideo)selectedListBoxItem.Content).VideoUrl != null)
            {
                videoUrl = ((TEDVideo)selectedListBoxItem.Content).VideoUrl.ToString();
            }
            else
                videoUrl = null;

            //TEDVideo t = new TEDVideo(title, author, imageUrl, viewUrl, videoUrl);
            TEDVideo t = (new TEDVideo
            {
                Title = title,
                Author = author,
                ViewUrl = viewUrl,
                ImageUrl = imageUrl,
                VideoUrl = videoUrl,
            });
            fvm.AddVideoToFavourite(t);
        }

        private void GestureDoubleTap_PlayVideo(object sender, GestureEventArgs e)
        {
            ListBoxItem selectedListBoxItem = this.listBox.ItemContainerGenerator.ContainerFromItem
                ((sender as Grid).DataContext) as ListBoxItem;

            if (selectedListBoxItem == null)
            {
                return;
            }
            string author = ((TEDVideo)selectedListBoxItem.Content).Author.ToString();
            string viewUrl = ((TEDVideo)selectedListBoxItem.Content).ViewUrl.ToString();
            Uri imageUrl = new Uri(((TEDVideo)selectedListBoxItem.Content).ImageUrl.ToString());
            string title = ((TEDVideo)selectedListBoxItem.Content).Title.ToString();
            string videoUrl = null;
            //TEDVideo t = new TEDVideo(title, author, imageUrl, viewUrl, videoUrl);
            TEDVideo t = (new TEDVideo
            {
                Title = title,
                Author = author,
                ViewUrl = viewUrl,
                ImageUrl = imageUrl,
                VideoUrl = videoUrl,
            });

            vvm.AddVideoToPlaylist(t);

            (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Video.xaml", UriKind.Relative));
            //Video v = new Video(t);
            

            MessageBox.Show("You double tap it");
            
        }

        private void AddVideoToPlayList_Click(object sender, RoutedEventArgs e)
        {
             ListBoxItem selectedListBoxItem = this.listBox.ItemContainerGenerator.ContainerFromItem
                ((sender as MenuItem).DataContext) as ListBoxItem;

            if (selectedListBoxItem == null)
            {
                return;
            }
            string author = ((TEDVideo) selectedListBoxItem.Content).Author.ToString();
            string viewUrl = ((TEDVideo) selectedListBoxItem.Content).ViewUrl.ToString();
            Uri imageUrl = new Uri(((TEDVideo) selectedListBoxItem.Content).ImageUrl.ToString());
            string title = ((TEDVideo) selectedListBoxItem.Content).Title.ToString();
            string videoUrl;
            if (((TEDVideo)selectedListBoxItem.Content).VideoUrl != null)
            {
                videoUrl = ((TEDVideo)selectedListBoxItem.Content).VideoUrl.ToString();
            }
            else
                videoUrl = null;

            //TEDVideo t = new TEDVideo(title, author, imageUrl, viewUrl, videoUrl);
            TEDVideo t = (new TEDVideo
            {
                Title = title,
                Author = author,
                ViewUrl = viewUrl,
                ImageUrl = imageUrl,
                VideoUrl = videoUrl,
            });
            vvm.AddVideoToPlaylist(t);
        }

        private void GestureTap_AddDefaultVideo(object sender, RoutedEventArgs e)
        {
            vvm.AddDefaultWMVVideoToPlaylist();
        }

    }
}
